home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / i18n / xpg3_wchar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.2 KB  |  126 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*---------------------------------------------------------------------------
  18.  *  xpg3_wchar.c  : sample program which shows the use of wide characters
  19.  *           for string manipulation which require locating specific
  20.  *           characters
  21.  *             Note : this is an XPG/3 way of internationalization
  22.  *    
  23.  *  this example reads a command line string and outputs two interleaved
  24.  *  strings (not useful in real life, but shows how to locate and
  25.  *  manipulate strings made of wide characters).
  26.  *
  27.  *  Author   : Yusuf Attarwala
  28.  *  Date     : May 93
  29.  *
  30.  *---------------------------------------------------------------------------*/
  31. #include <stdio.h>
  32. #include <stdarg.h>
  33. #include <ctype.h>
  34. #include <string.h>
  35. #include <locale.h>
  36. #include <wctype.h>
  37. #include <nl_types.h>
  38.  
  39.  
  40. nl_catd catd;
  41.  
  42. void
  43. usage(pname)
  44. char *pname;
  45. {
  46.     char *msg = catgets(catd,1,1,"Usage : %1$s string\n");
  47.     printf(msg,pname);
  48. }
  49.  
  50. myExit()
  51. {
  52.     catclose(catd);
  53.     exit();
  54. }
  55.  
  56. int
  57. main(argc, argv)
  58. register argc;
  59. char **argv;
  60. {
  61.     register int c;
  62.     register wchar_t *s,*o,*p;
  63.     wchar_t *first,*second;
  64.     int i,n;
  65.  
  66.     /*  i18n support */
  67.     /* establish a locale, cause locale database to be read in */
  68.     /* passing empty string will cause setlocale to look
  69.        for an environment variable LANG */
  70.  
  71.     (void)setlocale(LC_ALL, "");
  72.  
  73.     /* open a catalogue for messages, etc */
  74.     catd = catopen("xpg3_wchar.cat",0);
  75.  
  76.     /* sanity check */
  77.     if (argc < 2) {
  78.     usage(argv[0]);
  79.     myExit();
  80.     }
  81.  
  82.     /* allocate memory for the wide character string */
  83.     n = strlen(argv[1]);
  84.     if( !(s = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
  85.     printf(catgets(catd,1,2,"Could not allocate memory\n"));
  86.     myExit();
  87.     }
  88.  
  89.     /* convert string to wchar_t */
  90.     if(mbstowcs(s, argv[1], n) < 0) {
  91.     printf(catgets(catd,1,3,"Error in creating wide characters\n"));
  92.     myExit();
  93.     }
  94.  
  95.     /* allocate memory for second string */
  96.     if( !(p = (wchar_t *)calloc(n + 1, sizeof(wchar_t)))) {
  97.         printf(catgets(catd,1,2,"Could not allocate memory\n"));
  98.         myExit();
  99.     }
  100.  
  101.     o = s;
  102.  
  103.     first  = o;
  104.     second = p;
  105.  
  106.     /* collect odd and even letters in different strings */
  107.     for(i=0; *s; s += 2,i++) {
  108.     if (!*(s-1) && i > 1) break;    /* don't skip the null terminator */
  109.     *o++ = *s;
  110.     *p++ = *(s+1);
  111.     }
  112.  
  113.     /* null terminators */
  114.     mbtowc (o,"\0",1);
  115.     mbtowc (p,"\0",1);
  116.  
  117.     /* print out the interleaved strings */
  118.     printf(catgets(catd,1,4,"Here's the output : "));
  119.  
  120.     putws(first);
  121.     putws(second);
  122.  
  123.     /* close the catalog */
  124.     catclose(catd);
  125. }
  126.